import { routerRedux } from 'dva/router';
import {listRadioStation,updateRadioStationStatus,saveOrUpdateRadioStation,getRadioStationByRsId } from '../services/radioStation';

export default {
  namespace: 'radioStation',

  state: {
    loading: true,
    radioStationData: {
      errorCode:0,
      msg:"",
      data:{
        pageNo:null,
        pageCount:null,
        total:null,
        pageSize:null,
        latestTime:null,
        list:[]
      },
    },
  },

  effects: {
    *listRadioStation({ payload,callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(listRadioStation, payload);
      yield put({
        type: 'listRadioStationSuccess',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    *updateRadioStationStatus({ payload,callback }, { call, put }) {
      const response = yield call(updateRadioStationStatus, payload);
      if (callback) callback(response);
    },
    *saveOrUpdateRadioStation({ payload,callback }, { call, put }) {
      const response = yield call(saveOrUpdateRadioStation, payload);
      if (callback) callback(response);
    },
    *turnToRadioStationAddPage({ payload }, { put }) {
      if (payload) {
        yield put(routerRedux.push({
          pathname:'/radioStation/radioStationAdd',
          query:{
            rsId:payload.rsId,
            showDetail:payload.showDetail,
          }
        }));
      }else{
        yield put(routerRedux.push('/radioStation/radioStationAdd'));
      }
    },
    *turnToRadioStationListPage({ payload }, { put }) {
      yield put(routerRedux.push('/radioStation/radioStationList'));
    },
    *getRadioStationByRsId({ payload,callback }, { call, put }) {
      const response = yield call(getRadioStationByRsId, payload);
      if (callback) callback(response);
    },
  },

  reducers: {
    listRadioStationSuccess(state, action) {
      return {
        ...state,
        radioStationData: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
  },
};
